package com.imashimaro.serializer;

import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Collections;


public class NullValueSerializer {

    /**
     * 数字类型的null值序列化为 0
     */
    public static class NumberNullValueSerializer extends JsonSerializer<Object>{
        @Override
        public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
            gen.writeNumber(BigDecimal.ZERO);
        }
    }

    /**
     * 字符类型的Null值序列化为 ""
     */
    static class CharSequenceNullValueSerializer extends JsonSerializer<Object>{
        @Override
        public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
            gen.writeObject(StrUtil.EMPTY);
        }
    }

    /**
     * 布尔类型的null值默认序列化为false
     */
    static class BooleanNullValueSerializer extends JsonSerializer<Object> {
        @Override
        public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
            gen.writeObject(Boolean.FALSE);
        }
    }

    /**
     * 集合类型的null值序列化为[]
     */
    static class CollectionNullValueSerializer extends JsonSerializer<Object> {
        @Override
        public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
            gen.writeObject(Collections.emptyList());
        }
    }
}
